#! /usr/bin/env python
#coding=utf-8
from uliweb import expose
from sqlalchemy import *
from uliweb.orm import *
from uliweb.utils.pyini import Ini
from sqlalchemy.sql import select
from sqlalchemy.sql import and_, or_, not_
import time, sys
sys.path.insert(0, '../apps')

#from requirements.models import *
#from charts.models import *
#from charts.models import *
import datetime
from datetime import date
from get_month_quarter import *
from get_assignment_report_detail import *
#from __future__ import division

#db = get_connection('sqlite:///../database.db', debug=False)
#调用get_month_quarter.py中get_db_connection()获取数据库联接

#from uliweb.core.SimpleFrame import Dispatcher
#settings = {'ORM/DEBUG_LOG':True, 'ORM/AUTO_CREATE':False,'ORM/CONNECTION':'sqlite:///../database.db'}
#app = Dispatcher(apps_dir='../apps', start=False, default_settings=settings)

db = get_db_connection()

#meta = MetaData(db)
#db.metadata.reflect()

def run():

#    first_day,last_day = get_week()
#    first_day,last_day,month_view = get_month()
#    report_type = '2'
    
    #获取本月月初和月末时间
    first_day,last_day,quarter_view,quarter = get_quarter()
    report_type = '3'
    
    #判断本月末是否是季度末
    quarter = long(quarter)
    if(quarter <> 3) and (quarter <> 6) and (quarter <> 9) and (quarter <> 12):
        sys.exit()
    
    data =  get_assignment_report_detail(first_day,last_day,report_type)

    #print last_day,first_day

    req_model = get_model('assignment_report_detail')
    #清理本期数据，为本期数据存储做准备
    res_del = req_model.filter(and_(req_model.c.report_date == last_day,req_model.c.report_type == report_type))
    for row in res_del:
        row.requirements.clear()
#        row.systems.clear()
#        row.tx.clear()
        row.delete()
#    req_model.remove(and_(req_model.c.report_date == last_day,req_model.c.report_type == report_type))
    #开始保存本期数据
    for v,t in data.items():
        #print 't',t,'v',v
        r = req_model(**t)
        r.save()
                        


if __name__ == '__main__':
    try:
        db.begin()
        run()
        db.commit()
        log.info("finish")
    except Exception, e:
        db.rollback()
        log.exception(e)
        log.error('error')
        raise

    
#该函数为复制任务书中mannytomanny数据
def run_mannytomanny():
    
#    first_day,last_day = get_week()
#    first_day,last_day,month_view = get_month()
#    report_type = '2'
    
    #获取本月月初和月末时间
    first_day,last_day,quarter_view,quarter = get_quarter()
    report_type = '3'
    
    #判断本月末是否是季度末
    quarter = long(quarter)
    if(quarter <> 3) and (quarter <> 6) and (quarter <> 9) and (quarter <> 12):
        sys.exit()
    
    req_ass = get_model('assignment')
    req_detail = get_model('assignment_report_detail')
    #data[y]['requirements'] = requirements
    
    p = req_detail.filter(and_(req_detail.c.report_type == report_type,
                            req_detail.c.report_date == last_day))
                            
    for row in p:
#        print 'prow',row
#        res = req_ass.get(req_ass.c.id == row.ass)
#        ids = res.systems.ids()
        #row.systems.remove(*ids)
#        row.systems.add(*ids)
#        res = req_ass.get(req_ass.c.id == row.ass)
#        ids = res.tx.ids()
#        #row.systems.remove(*ids)
#        row.tx.add(*ids)
        res = req_ass.get(req_ass.c.id == row.ass)
        ids = res.requirements.ids()
        #row.systems.remove(*ids)
        row.requirements.add(*ids)
        
        

if __name__ == '__main__':
    try:
        db.begin()
        run_mannytomanny()
        db.commit()
        log.info("finish")
    except Exception, e:
        db.rollback()
        log.exception(e)
        log.error('error')
        raise
